package com.example.zbh.dsliao.ui.activty;

import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.constant.Constant;
import com.example.zbh.dsliao.presenter.RegisterPresenter;
import com.example.zbh.dsliao.ui.viewinterface.ViewRegisterInterface;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zbh on 15/11/16.
 */
public class RegisterActivity extends MVPBaseActivity<ViewRegisterInterface, RegisterPresenter> implements ViewRegisterInterface, View.OnClickListener {
    private EditText et_register_mail, et_register_psw, et_register_name;
    private Button regButton, backLoginBtn;
    private ImageView de_img_backgroud;
    private String userName = null;
    private  String userPsw = null;
    private  String userAgaPsw = null;


    @Override
    protected int getLayOut() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        et_register_mail = findeView(R.id.et_register_mail);
        et_register_psw = findeView(R.id.et_register_psw);
        et_register_name = findeView(R.id.et_register_name);
        regButton = findeView(R.id.button);//注册按钮
        backLoginBtn = findeView(R.id.backLoginBtn);//返回登录按钮
        backLoginBtn.getBackground().setAlpha(10);
        de_img_backgroud = findeView(R.id.de_img_backgroud);//背景
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.background_anim);
        de_img_backgroud.startAnimation(animation);//背景动画

    }

    @Override
    protected void initData() {
        regButton.setOnClickListener(this);
        backLoginBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                getUseInfos();//获取注册用户的信息
                break;
            case R.id.backLoginBtn://返回登录页面
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }


    }

    private void getUseInfos() {
        userName = et_register_mail.getText().toString().trim();
        userPsw = et_register_psw.getText().toString().trim();
        userAgaPsw = et_register_name.getText().toString().trim();
        Map<String, String> map = new HashMap<>();
        map.put(Constant.USER_NAME, userName);
        map.put(Constant.USER_PSW, userPsw);
        map.put(Constant.USER_AGA_PSW, userAgaPsw);
        createPresenter().startReg(map);
    }


    @Override
    protected RegisterPresenter createPresenter() {
        return new RegisterPresenter(this);
    }

    @Override
    public void showResult(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }

    /**
     * 注册成功 并跳转到登录页面
     * **/
    @Override
    public void ShowSucResult() {
        Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void showFailResult(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();

    }
}
